package top.lingkang.finalgateway.admin.dto;

import lombok.Data;
import lombok.experimental.Accessors;
import org.noear.snack.ONode;
import top.lingkang.finalgateway.admin.constants.StatusCode;

import java.io.Serializable;

/**
 * @author lingkang
 * Created in 2022/2/19
 */
@Data
@Accessors(chain = true)
public class ResponseResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private int code = StatusCode.OK;
    private String msg;
    private T data;

    public ResponseResult() {
    }

    public ResponseResult(String msg) {
        this.msg = msg;
    }

    public ResponseResult(T data) {
        this.data = data;
    }

    public ResponseResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseResult(String msg, T data) {
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult ok() {
        this.msg = "操作成功";
        return this;
    }

    public ResponseResult ok(String msg) {
        this.msg = msg;
        return this;
    }

    public ResponseResult ok(T data) {
        this.data = data;
        return this;
    }

    public ResponseResult ok(String msg, T data) {
        this.msg = msg;
        this.data = data;
        return this;
    }

    public ResponseResult fail(String msg) {
        code = StatusCode.FAIL;
        this.msg = msg;
        return this;
    }

    public ResponseResult fail(int code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public ResponseResult setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return ONode.load(this).toJson();
    }
}
